package com.molinari.coding.activities.register;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.molinari.androidstructure.activity.BaseActivity;
import com.molinari.coding.ControllerCoding;
import com.molinari.coding.R;
import com.molinari.coding.activities.first.FirstActivity;
import com.molinari.coding.commands.CommandInserisciUtente;
import com.molinari.coding.dati.domain.Utenti;
import com.molinari.coding.dati.domain.dao.WrapUtenti;

public class RegisterActivity extends BaseActivity {
	Button registra;
	
	@Override
	protected int getIdLayout() {
		return R.layout.register;
	}

	@Override
	protected void onCreateCustom(Bundle savedInstanceState) throws Exception {
		registra = loadButton(registra, R.id.btnRegisterPage);
		
		registra.setOnClickListener(new View.OnClickListener() {

			private TextView nome;
			private TextView cognome;
			private TextView username;
			private TextView password;

			@Override
			public void onClick(final View v) {
				nome = (TextView) loadEdit(nome, R.id.editRegiterName);
				cognome = (TextView) loadEdit(cognome, R.id.editRegiterSuername);
				username = (TextView) loadEdit(username, R.id.editRegiterUsername);
				password = (TextView) loadEdit(password, R.id.editRegiterPassword);
				if (nonEsistonoCampiVuoti(nome, cognome, username, password)) {
					WrapUtenti utente = new WrapUtenti(RegisterActivity.this);
					utente.setNome(nome.getText().toString());
					utente.setCognome(cognome.getText().toString());
					utente.setusername(username.getText().toString());
					utente.setpassword(password.getText().toString());
					try {
						if (ControllerCoding.getSingleton().invocaComando(new CommandInserisciUtente(RegisterActivity.this,utente))) {
							ControllerCoding.getSingleton().setUtenteLogin((Utenti) utente.getEntitaPadre());
							
							Toast.makeText(getApplicationContext(), R.string.utenteInserito, Toast.LENGTH_SHORT).show();
							final Intent i = new Intent(v.getContext(), FirstActivity.class);
							v.getContext().startActivity(i);
						}
					} catch (Exception e) {
						e.printStackTrace();
					}

				}
			}

			private boolean nonEsistonoCampiVuoti(final TextView nome, final TextView cognome, final TextView username,
					final TextView password) {
				return nome.getText() != null && cognome.getText() != null && username.getText() != null
						&& password.getText() != null && !nome.getText().toString().equals("")
						&& !cognome.getText().toString().equals("") && !username.getText().toString().equals("")
						&& !password.getText().toString().equals("");
			}
		});
	}	
	
//	@Override
//	protected Dialog onCreateDialog(final int id) {
//		AlertDialog.Builder builder = new AlertDialog.Builder(this);
//		builder.setMessage("Utente inserito con successo");
//		builder.setCancelable(true);
//		builder.setTitle("OK!");
//		return builder.create();
//	}

	@Override
	public int getIdMenu() {
		return 0;
	}

}
